from itertools import groupby

from odoo import api, fields, models, tools, _
from odoo.exceptions import ValidationError


class SbMoScrapReport(models.Model):
    _inherit = 's.mo.scrap.report'

    def action_feeding(self):
        res_ids = self.mapped('res_id')
        order_line = self.env['sale.order.line'].sudo().browse(res_ids)
        if type == 'sale':
            for line in order_line:
                if line.order_id.is_lock:
                    raise ValidationError('当前销售单已锁定，不允许投料。')
                if line.order_id.status == 'hold':
                    raise ValidationError('当前销售单已暂停，不允许投料。')
                if line.order_id.status == 'closed':
                    raise ValidationError('当前销售单已关闭，不允许投料。')
                if line.order_id.status == 'cancelled':
                    raise ValidationError('当前销售单已取消，不允许投料。')
        # erp锁单
        if order_line.filtered(lambda x: x.product_template_id.s_disable_down_card):
            raise ValidationError('当前物料已锁定不允许下卡')

        return super().action_feeding()
